<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form :inline="true" :model="queryForm">
        <el-form-item
          :label="
            $i18n.locale === 'en' ? dataList.eRoleName : dataList.RoleName
          "
        >
          <el-input
            v-model="queryForm.name"
            :placeholder="
              $i18n.locale === 'en' ? dataList.eRoleName : dataList.RoleName
            "
            @keyup.enter.native="onQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
        </el-form-item>
      </el-form>
      <!--操作栏-->
      <div class="base-content-operate">
        <el-button
          v-rights="'add'"
          icon="el-icon-plus"
          type="primary"
          @click="onNew"
          >{{ btn.Add }}</el-button
        >
      </div>
      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column align="center" :label="dataList.number" width="80">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column
          :label="
            $i18n.locale === 'en' ? dataList.eRoleName : dataList.RoleName
          "
        >
          <template slot-scope="scope">{{
            $i18n.locale === "en" ? scope.row.eName : scope.row.name
          }}</template>
        </el-table-column>
        <el-table-column :label="dataList.EnableOrNot">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              disabled
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="handleAddPermission(scope.row)"
                >{{ dataList.EditPermission }}</el-link
              >
              <el-link
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ btn.Edit }}</el-link
              >
              <el-link
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ btn.Delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="changePage"
      />
    </div>

    <PermissionRoleEdit
      ref="PermissionRoleEdit"
      :dialog-form-visible.sync="dialogFormVisible"
    />

    <PermissionMenuSelectShow
      ref="PermissionMenuSelectShow"
      :dialog-visible.sync="dialogMenuSelectVisible"
    />
  </div>
</template>

<script>
import {
  deleteRecord,
  getList,
} from "../../../api/Permission/PermissionRole.js";
import PermissionRoleEdit from "./PermissionRoleEdit.vue";
import PermissionMenuSelectShow from "../../../components/Permission/PermissionMenuSelectShow.vue";

export default {
  name: "PermissionRoleList",
  components: { PermissionRoleEdit, PermissionMenuSelectShow },
  data() {
    return {
      loading: true,
      list: null,
      queryForm: {
        name: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      dialogMenuSelectVisible: false,
    };
  },
  computed: {
    btn() {
      return this.$t("Permission.buttons");
    },
    dataList() {
      return this.$t("Permission.PermissionUser");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];

          this.list.forEach((element) => {
            element.status = element.status + ""; // 将status值转换为字符串,方便switch组件调用
          });

          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.PermissionRoleEdit.formData = this.Helper.deepCopy(
        this.$refs.PermissionRoleEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.PermissionRoleEdit.formData = this.Helper.deepCopy(rowData);
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
    handleAddPermission(rowData) {
      this.$refs.PermissionMenuSelectShow.roleId = rowData.id;
      // this.$refs.PermissionMenuSelectShow.getList();
      this.dialogMenuSelectVisible = true;
    },
  },
};
</script>
